import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex);
// 导入axios
import {getUserInfoAPI} from '@/api/channel'
export default new Vuex.Store({
  state: {
    // 保存token字符串
    token: "",
    // 用户信息id,username,nickname,email,user_pic
    userInfo: {},
  },
  getters: {
    // username(state){
    //   return state.userInfo.username
    // },
    nickname: state => state.userInfo.nickname, // 昵称
    username: state => state.userInfo.username, // 用户名
    user_pic: state => state.userInfo.user_pic // 用户头像
  },
  mutations: {
    // 保存token字符串
    updateToken(state, val) {
      state.token = val;
    },

    // 保存用户的信息
    updateUserInfo(state, info) {
      state.userInfo = info;
    },
  },
  actions: {
    // 定义初始化用户基本信息的 action 函数
    async getUserInfoActions(store) {
      const { data: res } = await getUserInfoAPI();
      if (res.code === 0) {
        store.commit("updateUserInfo", res.data);
      }
    },
  },
  modules: {},
  // 配置为 vuex 的插件
  plugins: [createPersistedState()],
});
